package com.hyy.language;

import android.text.TextUtils;

import com.blankj.utilcode.util.SPUtils;

import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author xu
 */
public class CommonUtils {


    public static Locale getLanguageLocal(String selectLanguage) {
        Locale locale;
        if (SelectLanguageDialog.EN_CODE.equals(selectLanguage)) {
            locale = Locale.US;
        } else if (SelectLanguageDialog.VI_CODE.equals(selectLanguage)) {
            locale = new Locale("vi", "rVN");
        } else if (SelectLanguageDialog.TH_CODE.equals(selectLanguage)) {
            locale = new Locale("th", "rTh");
        } else if (SelectLanguageDialog.ID_CODE.equals(selectLanguage)) {
            locale = new Locale("in", "rID");
        } else if (SelectLanguageDialog.ZH_CODE.equals(selectLanguage)) {
            locale = new Locale("zh", "rCN");
        } else if (SelectLanguageDialog.TW_CODE.equals(selectLanguage)) {
            locale = Locale.TAIWAN;
        } else {
            locale = Locale.US;
        }
        return locale;
    }

    public static void setCurLanguageName(String selectLanguage) {
        SPUtils.getInstance(SP_NAME).put("LanguageName", selectLanguage);
    }

    public static String getCurLanguageName() {
        return SPUtils.getInstance(SP_NAME).getString("LanguageName", SelectLanguageDialog.EN_CODE);
    }


    public static final String SP_NAME = "GAPSK_QM";

    public static void setCurLanguage(String selectLanguage) {
        SPUtils.getInstance(SP_NAME).put("Language", selectLanguage);
    }

    public static String getCurLanguage() {
        return SPUtils.getInstance(SP_NAME).getString("Language", SelectLanguageDialog.EN_CODE);
    }

    public static String getCurReleaseLanguage() {
        return SPUtils.getInstance(SP_NAME).getString("Language", "");
    }

    //至少包含一个数字
    public static boolean hasNumber(String s) {
        Pattern pattern = Pattern.compile("^(?![^0-9]+$).+$");
        Matcher m = pattern.matcher(s);
        return m.matches();
    }

    //至少包含一个字母
    public static boolean hasLetter(String s) {
        Pattern pattern = Pattern.compile("^(?![^a-zA-z]+$).+$");
        Matcher m = pattern.matcher(s);
        return m.matches();
    }

    public static String getLangUrl(String url) {
        String urlPath = "";
        if (!TextUtils.isEmpty(url)) {
            String curLang = getCurLanguage();
            if (url.contains("?")) {
                urlPath = url + "&lang=" + curLang;
            } else {
                urlPath = url + "?lang=" + curLang;
            }
        }
        return urlPath;
    }
}
